home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Files / ParseFullPathname / toolpfpn.c < prev   
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.6 KB  |  141 lines  |  [TEXT/MPS ]

  1. /*
  2. **
  3. **        ParseFullPathname
  4. **
  5. **            Takes a *complete* full pathname and turns it into a DirID and VRefNum pair.
  6. **            This does not handle partial pathnames because it assumes that the first element
  7. **            in the pathname is volume name.
  8. **
  9. **            to recurse or not to recurse, that is the question... (not)
  10. **
  11. **            This is an MPW Tool - build it accordingly.
  12. **
  13. **            The #if immediately preceding the #include's allows you to use pre-compiled headers, speeding
  14. **            the compile-debug-compile cycle significantly.  after you compile this the first time, set the
  15. **            1 to a 0 to use the dump file...
  16. **
  17. **            Neil Day
  18. **            MacDTS
  19. **            Apple Computer, Inc.
  20. **
  21. */
  22.  
  23. #if 0
  24. #include <stdio.h>                                                        // Compiler Interfaces
  25. #include <files.h>
  26. #include <String.h>
  27. #include <Strings.h>
  28. #include <Memory.h>
  29. #include <Errors.h>
  30. #pragma dump "CDump"
  31. #else
  32. #pragma load "CDump"
  33. #endif
  34.  
  35. typedef struct {                                                        // a handy info record
  36.     OSErr    error;
  37.     FSSpec    spec;
  38. }    specRec;
  39.  
  40. specRec *ParseFullPathname (char *pathname);                            // function prototypes
  41. char *firstElement (char *pn);
  42.  
  43. main (argc,argv)
  44. int argc;
  45. char **argv;
  46. {
  47.     specRec        *here;
  48.     
  49.     Debugger ();
  50.     
  51.     if (argc != 2) {
  52.         printf ("calling format: pfpn <pathname>\n");
  53.         return;
  54.     }
  55.     
  56.     here = ParseFullPathname (argv[1]);
  57.     if (!here->error)
  58.         printf ("Specified by the VRefNum %d and DirID %d\n", here->spec.vRefNum, here->spec.parID);
  59.     else
  60.         printf ("There was an error %d while getting pathname\n",here->error);
  61.     
  62.     DisposPtr ((Ptr) here);
  63. printf ("disposed of a specRec\n");
  64.     Debugger ();
  65. }
  66.  
  67. char *firstElement (char *pn)
  68. {
  69.     int             cChar = 0;
  70.     char            *substring;
  71.     
  72.     while (*(pn+cChar) != ':' && *(pn + cChar) != '\0')                    // find the first element
  73.         ++cChar;
  74.     
  75.     if (*(pn + cChar)) {
  76.         substring  = NewPtrClear (++cChar+1);                            // allocate space result
  77. printf ("Allocated a string\n");
  78.         BlockMove ((Ptr) pn, (Ptr) substring, cChar);                    // move the first element into result
  79.         BlockMove ((Ptr) pn+cChar, (Ptr) pn, strlen(pn) - cChar+1);        // delete the first element if there is one
  80.     } else
  81.         substring = NULL;                                                // no result    
  82.         
  83.     return substring;
  84. }
  85.  
  86. specRec *ParseFullPathname (char *pathname)
  87. {
  88.     char             *current;
  89.     specRec            *rRec;
  90.     OSErr            err;
  91.     HParmBlkPtr        vInfo;
  92.     CInfoPBPtr        dInfo;
  93.  
  94.     rRec = (specRec *) NewPtrClear (sizeof (specRec));
  95.     rRec->spec.parID = fsRtDirID;                                        // start at the top of the volume
  96. printf ("Allocated a specRec\n");
  97.     current = firstElement (pathname);
  98.     if (!current) {
  99.         rRec->error = fnfErr;
  100.         return rRec;
  101.     }
  102.  
  103.     vInfo = (HParmBlkPtr) NewPtrClear (sizeof (HParamBlockRec));        // resolve vRefNum
  104. printf ("Allocated a paramblock\n");
  105.     vInfo->volumeParam.ioNamePtr = c2pstr (current);
  106.     vInfo->volumeParam.ioVolIndex = -1;
  107.     err = PBHGetVInfo (vInfo,false);
  108.     rRec->spec.vRefNum = vInfo->volumeParam.ioVRefNum;
  109.     DisposPtr ((Ptr) vInfo);
  110. printf ("Disposed of a paramblock\n");
  111.     if (current){
  112.         DisposPtr ((Ptr) current);
  113. printf ("Disposed of a string\n");
  114.     }
  115.     if (err) {
  116.         rRec->error = err;
  117.         return rRec;
  118.     }
  119.     
  120.     dInfo = (CInfoPBPtr) NewPtrClear (sizeof (CInfoPBRec));
  121. printf ("Allocated a Cparamblock\n");
  122.     
  123.     while (current = firstElement(pathname)) {                            // Walk down the pathname
  124.         *(current + (strlen(current)-1)) = '\0';                        // get rid of the colon
  125.         dInfo->dirInfo.ioFDirIndex = 0;
  126.         dInfo->dirInfo.ioDrDirID = rRec->spec.parID;
  127.         dInfo->dirInfo.ioVRefNum = rRec->spec.vRefNum;
  128.         dInfo->dirInfo.ioNamePtr = c2pstr (current);
  129.         err = PBGetCatInfo (dInfo,false);
  130.         DisposPtr ((Ptr) current);
  131. printf ("Disposed of a string\n");
  132.         if (err) {
  133.             rRec->error = err;
  134.             break;
  135.         }
  136.         rRec->spec.parID = dInfo->dirInfo.ioDrDirID;
  137.     }
  138.     DisposPtr ((Ptr) dInfo);
  139. printf ("Disposed of a Cparamblock\n");
  140.     return rRec;
  141. }